//
//  RegisterViewController.m
//  TravelFollowYourHeart
//
//  Created by ENERGY on 2017/12/26.
//  Copyright © 2017年 Lwx. All rights reserved.
//

#import "RegisterViewController.h"
#import "MineViewController.h"
#import "AppTabBarController.h"

static int i = 29;

@interface RegisterViewController ()
@property(nonatomic,strong) UITextField *telephoneTextField;
@property(nonatomic,strong) UITextField *pwdTextField;
@property(nonatomic,strong) UITextField *numberTextField;
@property(nonatomic,strong) UITextField *againPwdTextField;
@property(nonatomic,strong) UIButton *getNmuber;// 定时器
@property(nonatomic,strong) NSTimer *timmer;
@property(nonatomic,strong) UIButton *qurRenBen;
@property(nonatomic,strong) NSString *code;
- (void)test:(NSTimer *)timmer;
@end

@implementation RegisterViewController(Networkin)
//确认按钮
- (void)registerAccount{
    NSDictionary * dic = @{
                           @"mobile":self.telephoneTextField.text,
                           @"pass":self.pwdTextField.text,
                           @"surePass":self.againPwdTextField.text,
                           @"smsCode":@([self.numberTextField.text intValue])
                           };
    [[HttpRequest shardWebUtil] postNetworkRequestURLString:Url(@"/login/register") andParas:dic andTransferGottenData:^(id obj, NSError *error) {
        
        if (error) {
            [[HUDHelper sharedInstance] tipMessage:obj[@"msg"]];
        }else{
            if ([obj[@"code"] integerValue] == 100000){
                [[HUDHelper sharedInstance] tipMessage:obj[@"msg"]];
                [self.navigationController popViewControllerAnimated:YES];
            }else{
                [[HUDHelper sharedInstance] tipMessage:obj[@"msg"]];
            }
        }
    }];
}
//获取验证码
- (void)sendVerificationCode:(NSString *)num{
    NSDictionary * dic = @{
                           @"mobile":num,
                           };
    [[HttpRequest shardWebUtil] postNetworkRequestURLString:Url(@"/login/getVertify") andParas:dic andTransferGottenData:^(id obj, NSError *error) {
        if (error) {
            [[HUDHelper sharedInstance] tipMessage:obj[@"msg"]];
        }else{
            if ([obj[@"code"] integerValue] == 100000){
//                [[HUDHelper sharedInstance] tipMessage:obj[@"msg"]];
                self.getNmuber.enabled = NO;
                [self.getNmuber setBackgroundColor:[UIColor grayColor]];
                //初始化定时器
                self.timmer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(test:) userInfo:nil repeats:YES];
            }else{
                [[HUDHelper sharedInstance] tipMessage:obj[@"msg"]];
            }
        }
    }];
}
@end
@implementation RegisterViewController
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = getColor(bgColor);
    
    [self setUpView];
}

- (void)setUpView{
    //白色背景
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 60, SCREEN_WIDTH, 165)];
    bgView.backgroundColor = getColor(whiteColor);
    
    //用户图标
    UIImageView *userImgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 15, 20, 20)];
    userImgView.image = [UIImage imageNamed:@"login_icon_accaunt"];
    
    //账号
    self.telephoneTextField = [[UITextField alloc] initWithFrame:CGRectMake(userImgView.frame.size.width+userImgView.frame.origin.x+20, 0, SCREEN_WIDTH-55, 40)];
    self.telephoneTextField.font = DEF_FontSize_14;
    self.telephoneTextField.textColor = getColor(blackColor);
    self.telephoneTextField.textAlignment = NSTextAlignmentLeft;
    self.telephoneTextField.placeholder = @"请输入手机号";
    self.telephoneTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.telephoneTextField.clearsOnBeginEditing = YES;
    
    UIView *lineView1 = [[UIView alloc] initWithFrame:CGRectMake(0, _telephoneTextField.frame.size.height+1, SCREEN_WIDTH, 1)];
    lineView1.backgroundColor = getColor( dividerColor);
    
    //验证码
    self.numberTextField = [[UITextField alloc] initWithFrame:CGRectMake(_telephoneTextField.frame.origin.x, 42, _telephoneTextField.frame.size.width-105, 40)];
    self.numberTextField.font = DEF_FontSize_14;
    self.numberTextField.textColor = getColor(blackColor);
    self.numberTextField.textAlignment = NSTextAlignmentLeft;
    self.numberTextField.placeholder = @"请输入验证码";
    self.numberTextField.secureTextEntry = YES;
    self.numberTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.numberTextField.clearsOnBeginEditing = YES;
    
    self.getNmuber = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-95, lineView1.frame.origin.y+8, 90, 25)];
    self.getNmuber.layer.cornerRadius = 12;
    self.getNmuber.layer.masksToBounds = YES;
    self.getNmuber.titleLabel.font=[UIFont systemFontOfSize:14];
    [self.getNmuber setTitle:@"获取验证码" forState:UIControlStateNormal];
    [self.getNmuber setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.getNmuber.backgroundColor=getColor(@"ffbb02");
    [self.getNmuber addTarget:self action:@selector(sendNumber) forControlEvents:UIControlEventTouchUpInside];
    
    //线
    UIView *lineView2 = [[UIView alloc] initWithFrame:CGRectMake(0, _numberTextField.frame.origin.y+_numberTextField.frame.size.height+1, SCREEN_WIDTH, 1)];
    lineView2.backgroundColor = getColor( dividerColor);
    
    //密码图片
    UIImageView *pwdImgView = [[UIImageView alloc] initWithFrame:CGRectMake(userImgView.frame.origin.x, lineView2.frame.origin.y+11, 20, 20)];
    pwdImgView.image = [UIImage imageNamed:@"longin_password"];
    
    //密码
    self.pwdTextField = [[UITextField alloc] initWithFrame:CGRectMake(_telephoneTextField.frame.origin.x, lineView2.frame.origin.y+1, _telephoneTextField.frame.size.width, _telephoneTextField.frame.size.height)];
    self.pwdTextField.font = DEF_FontSize_14;
    self.pwdTextField.textColor = getColor(blackColor);
    self.pwdTextField.textAlignment = NSTextAlignmentLeft;
    self.pwdTextField.placeholder = @"请输入密码";
    self.pwdTextField.secureTextEntry = YES;
    self.pwdTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.pwdTextField.clearsOnBeginEditing = YES;
    
    //线
    UIView *lineView3 = [[UIView alloc] initWithFrame:CGRectMake(0, _pwdTextField.frame.origin.y+_pwdTextField.frame.size.height+1, SCREEN_WIDTH, 1)];
    lineView3.backgroundColor = getColor( dividerColor);
    
    //密码图片
    UIImageView *againPwdImg = [[UIImageView alloc] initWithFrame:CGRectMake(userImgView.frame.origin.x, lineView3.frame.origin.y+11, 20, 20)];
    againPwdImg.image = [UIImage imageNamed:@"longin_password"];
    
    //密码
    self.againPwdTextField = [[UITextField alloc] initWithFrame:CGRectMake(_telephoneTextField.frame.origin.x, lineView3.frame.origin.y+1, _telephoneTextField.frame.size.width, _telephoneTextField.frame.size.height)];
    self.againPwdTextField.font = DEF_FontSize_14;
    self.againPwdTextField.textColor = getColor(blackColor);
    self.againPwdTextField.textAlignment = NSTextAlignmentLeft;
    self.againPwdTextField.placeholder = @"请再次输入密码";
    self.againPwdTextField.secureTextEntry = YES;
    self.againPwdTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.againPwdTextField.clearsOnBeginEditing = YES;
    
    [self.view addSubview:bgView];
    [bgView addSubview:userImgView];
    [bgView addSubview:_telephoneTextField];
    [bgView addSubview:_numberTextField];
    [bgView addSubview:_pwdTextField];
    [bgView addSubview:_againPwdTextField];
    [bgView addSubview:againPwdImg];
    [bgView addSubview:pwdImgView];
    [bgView addSubview:lineView1];
    [bgView addSubview:lineView2];
    [bgView addSubview:lineView3];
    [bgView addSubview:_getNmuber];
    
    self.qurRenBen = [[UIButton alloc] initWithFrame:CGRectMake(15, SCREEN_HEIGHT-74-64, SCREEN_WIDTH-30, 50)];
    self.qurRenBen.layer.cornerRadius = 20;
    self.qurRenBen.layer.masksToBounds = YES;
    self.qurRenBen.titleLabel.font=[UIFont systemFontOfSize:20];
    [self.qurRenBen setTitle:@"确认" forState:UIControlStateNormal];
    [self.qurRenBen setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.qurRenBen.backgroundColor=getColor(@"ffbb02");
    [self.qurRenBen addTarget:self action:@selector(quRen:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.qurRenBen];
    
}

- (void)sendNumber{
    NSLog(@"发送验证码");
    if (self.telephoneTextField.text.length == 0) {
        [[HUDHelper sharedInstance]tipMessage:@"请输入手机号"];
        return;
    }
    [self.view endEditing:YES];
    [self sendVerificationCode:self.telephoneTextField.text];
    
    //    self.getNmuber.enabled = NO;
    //    [self.getNmuber setBackgroundColor:[UIColor grayColor]];
    //    //初始化定时器
    //    _timmer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(test:) userInfo:nil repeats:YES];
}

- (void)test:(NSTimer *)timmer{
    NSString *str = [NSString stringWithFormat:@"%d后重试",i];
    [self.getNmuber setTitle:str forState:UIControlStateDisabled];
    i--;
    if (i == -1) {
        [timmer invalidate];
        self.getNmuber.enabled = YES;
        [self.getNmuber setTitle:@"发送验证码" forState:UIControlStateNormal];
        [self.getNmuber setBackgroundColor:getColor(@"ffbb02")];
        i = 29;
    }
}

- (void)quRen:(UIButton *)qurRenBen{
    NSLog(@"确认");
    if (self.telephoneTextField.text.length == 0) {
        [[HUDHelper sharedInstance]tipMessage:@"请输入手机号"];
        return;
    }
    if (self.numberTextField.text.length == 0) {
        [[HUDHelper sharedInstance]tipMessage:@"请输入验证码"];
        return;
    }
    if (self.pwdTextField.text.length == 0) {
        [[HUDHelper sharedInstance]tipMessage:@"请输入密码"];
        return;
    }
    if (self.againPwdTextField.text.length == 0) {
        [[HUDHelper sharedInstance]tipMessage:@"请再次输入密码"];
        return;
    }
    //    if ([self.title isEqualToString:@"注册"]) {
    //        [self zuCheGetMessage];
    //    }else{
    //        [self wangJiMiMaGetMessage];
    //    }
    [self registerAccount];
}

- (void)backBtn{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end

